package com.shop;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.mapreduce.lib.db.DBWritable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;

public class Shop implements Writable, DBWritable {
    private String userId;
    private String shopId;
    private double value;

    public Shop() {
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    @Override
    public String toString() {
        return "Shop{" +
                "userId='" + userId + '\'' +
                ", shopId='" + shopId + '\'' +
                ", value=" + value +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Shop shop = (Shop) o;
        return Double.compare(value, shop.value) == 0 && Objects.equals(userId, shop.userId) && Objects.equals(shopId, shop.shopId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userId, shopId, value);
    }

    public void setShopId(String shopId) {
        this.shopId = shopId;
    }

    public void setValue(double value) {
        this.value = value;
    }

    public String getUserId() {
        return userId;
    }

    public String getShopId() {
        return shopId;
    }

    public double getValue() {
        return value;
    }

    public Shop(String userId, String shopId, double value) {
        this.userId = userId;
        this.shopId = shopId;
        this.value = value;
    }

    @Override
    public void write(DataOutput dataOutput) throws IOException {
        dataOutput.writeUTF(userId);
        dataOutput.writeUTF(shopId);
        dataOutput.writeDouble(value);
    }

    @Override
    public void readFields(DataInput dataInput) throws IOException {
        this.shopId=dataInput.readUTF();
        this.userId=dataInput.readUTF();
        this.value=dataInput.readDouble();
    }

    @Override
    public void write(PreparedStatement preparedStatement) throws SQLException {
        preparedStatement.setString(1,userId);
        preparedStatement.setString(2,shopId);
        preparedStatement.setDouble(3,value);
    }

    @Override
    public void readFields(ResultSet resultSet) throws SQLException {
        this.userId = resultSet.getString("user_id");
        this.shopId = resultSet.getString("shops_id");
        this.value = resultSet.getDouble("recommand_value");
    }
}
